declare var Snap: any, mina: any;
import { Component, OnInit, DoCheck, Input, OnChanges, SimpleChange, ViewChild, ElementRef, NgModule } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { Http, Response, Headers, RequestOptions, URLSearchParams } from '@angular/http';
import { FormArray, FormBuilder, FormGroup } from '@angular/forms';
// import LCalendar from "../../assets/js/LCalendar.js";
import LCalendar from "../../assets/js/lCalendar (2).js";
// import { ActivatedRoute, Params} from '@angular/router';
import { ConmonService } from "../../service/common.service";
// declare var $: any;
import * as $ from 'jquery';
export class PersonScreening {
  // 员工姓名
  // employeename: string;
  // 员工编号
  // employeeNumber: string;
  // 报工日期
  WorkDate: string;
  //  报工提交日期
  SubmitDate: string;
  // 报工类型
  WorkType: string;
  // 项目名称
  ProName: string;
  //  项目编号
  ProId: string;
  // 工作内容
  WorkContent: string;
  // 消耗工时
  ConsumeTime: any;
  //审核意见
  CheckOpin: any;
  //总页数
  AccessPages: any;
}
@Component({
  selector: 'app-person-screening',
  templateUrl: './person-screening.component.html',
  styleUrls: ['./person-screening.component.scss']
})
@NgModule({
  providers: [
    ConmonService
  ]
})
export class PersonScreeningComponent implements OnInit {
  @Input() PersonScreening: PersonScreening = {
    // employeename: '',
    // employeeNumber: '',
    WorkDate: '',
    SubmitDate: '',
    ProName: '',
    WorkType: '',
    ProId: '',
    WorkContent: '',
    ConsumeTime: '',
    CheckOpin: '',
    AccessPages: '1'
  };
  private iswork: boolean = false;
  private isAddWork: boolean = false;
  private isleave: boolean = false;
  private isFishesd: boolean = false;
  private agree: any;
  private reject: any;
  private flag: any;
  private projectNameFlag: boolean = false;
  private projectIdFlag: boolean = false;
  private dataObj:any;
  private dataList:any;
  private proType:string = '';
  ngAfterViewInit() {
    
  };
  choiceWorkType($event) {
    var val = $event.target.value;
    if (val === '工作') {
      this.iswork = true;
      this.isleave = false;
      this.isAddWork = false;
      this.PersonScreening.WorkType = '0';

    } else if (val === '加班') {
      // console.log(this);
      this.isAddWork = true;
      this.isleave = false;
      this.iswork = false;
      this.PersonScreening.WorkType = '1';
    } else if (val === '请假') {
      //   console.log(this);
      this.isleave = true;
      this.isAddWork = false;
      this.iswork = false;
      this.PersonScreening.WorkType = '2';
    }
  };
  choiceCheckOpin($event) {
    var val = $event.target.value;
    if (val === '同意') {
      this.agree = true;
      this.reject = false;
      this.PersonScreening.CheckOpin = '0';
    } else {
      this.agree = false;
      this.reject = true;
      this.PersonScreening.CheckOpin = '1'
    }
  };
  //调项目名称的搜索，改变搜索结果的样式
  keyChangeColor(value: string,obj) {
    this.projectNameFlag = true;
    console.log(obj.target.nextElementSibling);
    this.dataObj = {
      SearchProType: '0',
      ProName: value,
      ProId: '',
      ProMana: ''
    };
   this.dataList = this.service.withBodyData(this.dataObj,'joinWorkProListInfo')
    console.log(this.dataList);
    // let array = value.split(""); //分割
    // let  dsa=this.projectNameSearch.nativeElement;//获取全部商品
    setTimeout(function(){
      let dsa = $(obj.target.nextElementSibling).find('li');
      for (let t = 0; t < dsa.length; t++) {
        let val = dsa.eq(t).text();
        console.log(val);
        let x = val.replace(value,"<span class='col_Color' style='color:#54a3ff;'>"+value+"</span>");
        $(obj.target.nextElementSibling).find('li').eq(t).html(x);
      };
    },0)
  };
  //为项目名称搜索出来的结果赋值
  addProjectName (obj) {
    console.log(obj);
    this.PersonScreening.ProName = obj.ProName;
    this.PersonScreening.ProId = obj.ProId;
    this.proType = obj.DailyWorkType;
    this.projectNameFlag = false;
    // this.dataList = '';
  };
  //项目名称失去焦点
  pronameBlur () {
    let proNameValue = $("#projectName").val();
    // let proIdValue = $("#projectNumber").val();
    this.dataObj = {
      SearchProType: '0',
      ProName: proNameValue,
      ProId: '',
      ProMana: ''
    };
    let resData = this.service.withBodyData(this.dataObj, 'joinWorkProListInfo');
    console.log(resData);
    if (resData.body.ProInfo.length === 1) {
      this.PersonScreening.ProId = resData.body.ProInfo[0].ProId;
      this.proType = resData.body.ProInfo[0].DailyWorkType;
    }
    let _this = this;
    setTimeout(function(){
      _this.projectNameFlag = false;
    },301)
    // this.dataList = '';
  };
  //项目编号搜索
  projectIdSearch (value: string,obj) {
    this.projectIdFlag = true;
    console.log(value);
    this.dataObj = {
      SearchProType: '0',
      ProName: '',
      ProId: value,
      ProMana: ''
    };
   this.dataList = this.service.withBodyData(this.dataObj,'joinWorkProListInfo')
    console.log(this.dataList);
    // let array = value.split(""); //分割
    // let  dsa=this.projectNameSearch.nativeElement;//获取全部商品
    setTimeout(function(){
      let dsa = $(obj.target.nextElementSibling).find('li');
      for (let t = 0; t < dsa.length; t++) {
        let val = dsa.eq(t).text();
        console.log(val);
        let x = val.replace(value,"<span class='col_Color' style='color:#54a3ff;'>"+value+"</span>");
        $(obj.target.nextElementSibling).find('li').eq(t).html(x);
      };
    },0)
  };
  //项目编号失去焦点 
  projectNumberBlur () {
    // this.dataList = '';
    // let proNameValue = $("#projectName").val();
    let proIdValue = $("#projectNumber").val();
    this.dataObj = {
      SearchProType: '0',
      ProName: '',
      ProId: proIdValue,
      ProMana: ''
    };
    let resData = this.service.withBodyData(this.dataObj, 'joinWorkProListInfo');
    console.log(resData);
    if (resData.body.ProInfo.length === 1) {
      this.PersonScreening.ProName = resData.body.ProInfo[0].ProName;
      this.proType = resData.body.ProInfo[0].DailyWorkType;
    }
    let _this = this;
    setTimeout(function(){
      _this.projectIdFlag = false;
    },301)
  };
  //为项目编号搜索出来的结果添加赋值
  addProjectId (obj) {
    this.PersonScreening.ProName = obj.ProName;
    this.PersonScreening.ProId = obj.ProId;
    this.proType = obj.DailyWorkType;
    // this.dataList = '';
    this.projectIdFlag = false;
  };
  // 发送请求，从后台传输数据
  results: string[];
  ngDoCheck() {
  }
  ngOnInit() {
    // 初始化获取数据信息
    if (document.getElementById('employeeDate')) {
      // 我的报工添加的选择报工日期日历插件调用
      var calendar = new LCalendar();
      calendar.init({
        'trigger': '#employeeDate', //标签id
        'type': 'date', //date 调出日期选择 datetime 调出日期时间选择 time 调出时间选择 ym 调出年月选择,
        'minDate': '1900-1-1', //最小日期
        'maxDate': new Date().getFullYear() + 10 + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate() //最大日期
      });
    }
    // 我的报工添加的选择报工日期日历插件调用
    if (document.getElementById('DateSubmit')) {
      var DateSubmit = new LCalendar();
      DateSubmit.init({
        'trigger': '#DateSubmit', //标签id
        'type': 'date', //date 调出日期选择 datetime 调出日期时间选择 time 调出时间选择 ym 调出年月选择,
        'minDate': '1900-1-1', //最小日期
        'maxDate': new Date().getFullYear() + 10 + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate() //最大日期
      });
    }
  }

  constructor(private router: Router, private http: HttpClient, private routerIonfo: ActivatedRoute, private service: ConmonService) {
    this.flag = this.routerIonfo.snapshot.params["flag"];
    console.log(typeof this.flag);
  }

  // 报工添加点击确定判断是否跳转
  sureShow(flag): void {
    //  这里要传当前页面的数据给后台
    console.log(this.PersonScreening);
    this.service.personScreeningPostData = this.PersonScreening;
    console.log(flag);
    console.log(typeof flag);
    if (flag === 'true') {
      this.service.savePersonScreeningData(this.PersonScreening, 'FiltrateMyApplyWork',flag);
      // this.savePersonScreeningData(this.PersonScreening, 'FiltrateMyApplyWork',flag);
    } else if (flag === 'false') {
      this.service.savePersonScreeningData(this.PersonScreening, 'FiltrateMyDailyWork',flag);
      // this.savePersonScreeningData(this.PersonScreening, 'FiltrateMyDailyWork',flag);
    }
    // this.service.personScreeningData = 'responseData';
    // this.router.navigate(['/PersonTatal', flag]);

  };
  savePersonScreeningData (dataObj,serviceName,flag) {
    // let d = new Date();
    // // d.setTime(d.getTime() + (1 * 24 * 60 * 60 * 1000));
    // d.setTime(d.getTime() + (5 * 1000));
    // let expires = "expires=" + d.toUTCString();
    // document.cookie = "ATOKEN=" + this.token + ";domain=" + this.domainVal + ";path=/;" + expires + ";";
    let _this = this;
    dataObj = JSON.stringify(dataObj);
    let dataList;
    $.ajax({
        async: false, // 同步请求 false，异步请求 true
        type: "POST", // 请求类型，POST、GET
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        url: _this.service.url, // 请求的地址
        data: {crmData: "{'head': {'ServName':"+"'"+serviceName+"'"+",'SeqNo':'123123123123123123123123','employId':'000123'},'body':"+dataObj+"}"},
        // data: {crmData: "{'head': {'ServName':"+"'"+serviceName+"'"+",'SeqNo':'123123123123123123123123','employId':"+"'"+_this.employId+"'"+"},'body':"+dataObj+"}"},
        dataType: 'json',
        success: function(responseData) {
            console.log(responseData);
            if (responseData.head.Result === 'S') {
                _this.service.personScreeningData = responseData;
                _this.router.navigate(['./PersonTatal', flag]);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            // 处理请求发生异常
            console.log("请求发生异常！" + textStatus + errorThrown);
        },
    });
};
  // 重置方法
  resetBtn() {
    for (let item in this.PersonScreening) {
      this.PersonScreening[item] = ''
    };
  }
}